/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package embeddedpaint.user_interface;

import embeddedpaint.model.layers.ALayer;
import embeddedpaint.model.layers.LayerText;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

public class ImageListCellRenderer implements ListCellRenderer
{

    private static final String M_LABEL_TEXT_LAYER          = "(TXT) ";
    private static final String M_LABEL_IMAGE_LAYER         = "(IMG) ";
    private static final String M_LABEL_UNSUPPORTED_LAYER   = "???";
    private static final Color  M_FOREGROUND_COLOR          = Color.WHITE;
    private static final Color  M_BACKGROUND_COLOR          = Color.WHITE;
    private static final Color  M_BACKGROUND_COLOR_SELECTED = Color.CYAN;
    private static final int    M_ICON_SIZE                 = 20;

    @Override
    public Component getListCellRendererComponent (JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
    {
        if (value instanceof ALayer)
        {
            ALayer layer = (ALayer) value;
            String name  = layer.getM_name();
            
            if (layer instanceof LayerText)
            {
                name = M_LABEL_TEXT_LAYER + name;
            }
            else
            {
                name = M_LABEL_IMAGE_LAYER + name;
            }

            JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
            
            panel.add(new JLabel(name, new ImageIcon(layer.getIconWithBorder(M_ICON_SIZE, M_ICON_SIZE)), FlowLayout.LEFT));

            Component component = panel;
            component.setForeground(M_FOREGROUND_COLOR);
            component.setBackground(isSelected ? M_BACKGROUND_COLOR_SELECTED : M_BACKGROUND_COLOR);
            
            return component;
        }
        else
        {
            return new JLabel(M_LABEL_UNSUPPORTED_LAYER);
        }
    }
}
